<?php

namespace App\Http\Controllers\Activity;

use Illuminate\Support\Facades\Storage;
use EasyWeChat\Foundation\Application;
use App\Morefun\Wechat\Service as WechatService;
use Illuminate\Http\Request;
use Ramsey\Uuid\Uuid;

class WechatController
{
    protected $loader;

    public function toActivityPage(Request $request, WechatService $service)
    {
        $id = $request->query('id');

        if (!$id) {
            return response()->failed('没有id你玩儿个鸡巴');
        }

        return $service->gatherUserAndRedirect($id);
    }

    public function toShared(Request $request, WechatService $service)
    {
        $id = $request->query->get('id');

        if (!Uuid::isValid($id)) {
            return "错误的活动id";
        }

        $url = $service->makeSilentlyAuthorizedUrl($id, true);

        if (empty($url)) {
            abort(404);
        }

        return redirect($url);
    }

    /*
         * 微信卡券使用参照 https://www.easywechat.com/docs/3.x/zh-CN/card
         */
    public function createWechatCard(){
        $app = new Application([]);
        //生成卡券实例
        $card = $app->card;
    }

    /*
     * 发送红包接口
     */
    public function sendRedPack(Request $request){
        $options = [
            'payment' => [
                'app_id'           =>'wx7fabb65e344e63e5',
                'merchant_id'        => '1329936301',
                'key'                => 'Szmg2018Szmg2018Szmg2018Szmg2018',
                'cert_path'          => storage_path('apiclient_cert.pem'),
                'key_path'           => storage_path('apiclient_key.pem'),
            ],
        ];
        $app = new Application($options);
        $luckyMoney = $app->lucky_money;
        $luckyMoneyData = [
            'mch_billno'       => 'xy123456', //商户订单号
            'send_name'        => '测试红包',
            're_openid'        => 'oInqVxLgY5ubna3e1FAU7cflNn30',//'oxTWIuGaIt6gTKsQRLau2M0yL16E', //微信用户openid
            'total_num'        => 1,  //普通红包固定为1，裂变红包不小于3
            'total_amount'     => 1,  //单位为分，普通红包不小于100，裂变红包不小于300
            'wishing'          => '祝福语',
            //'scene_id'        =>'PRODUCT_1',
            //'client_ip'        => '192.168.0.1',  //可不传，不传则由 SDK 取当前客户端 IP
            'act_name'         => '测试活动',
            'remark'           => '测试备注',
        ];
        return $luckyMoney->send($luckyMoneyData, \EasyWeChat\Payment\LuckyMoney\API::TYPE_NORMAL);
    }

    /*
     * 上传微信商户支付证书
     */
    public function loadWechatKeyPath(Request $request){
        $filepath = storage_path('app/path/'.$_COOKIE['group_id'].'/');
        $files = $request->file('file');
        if($files->isValid()){
            if($files->getClientOriginalExtension() != 'pem') return response()->failed('请上传正确的证书');
            $bool = $files->move($filepath,$files->getClientOriginalName());
            if(!$bool) return response()->failed('文件上传失败');
            return response()->success($filepath);
        }else{
            return response()->failed('文件上传失败--'.$files->getErrorMessage());
        }
    }
}
